//              this is kpDemo's header file
//              author: Michael Lee
//              email: michael.20@qq.com

#ifndef KPDEMO_H
#define KPDEMO_H

#include "publicFun.h"
#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QMessageBox>
#include <QTextCodec>
#include <QWebFrame>
#include <QLabel>
#include <QSettings>
#include <QFile>
#include <QFileDialog>
#include<QWebFrame>
#include <QDebug>

namespace Ui {
    class kpDemo;
}
enum EnumStackUI
{
    SW_WEBVIEW_UI=0,
    SW_KUAIPAN_UI
};

typedef struct _auTokenJson
{
    QString auToken;
    QString auTokenSecret;
    QString usrID;
    QString usrDirID;
    QString mUploadNode;
}struAuToken;

typedef struct _usrInfoJson
{
    QString maxFileSize;
    QString quotaTotal;
    QString quotaUsed;
    QString usrName;
}struUsrInfo;

typedef struct _metaDataPara
{
    bool    useDataPara;
   // int     file_limit;     //默认0  不分页
    int     page;       //int	默认0 - 不分页；和page_size 一起使用；从 1 开始，表示显示第N页的文件
    int     page_size;  //int	默认20；page 不等于 0才有效；表示每页显示的文件内容。 最大值为512
   // QString filter_ext; //string 过滤特定扩展名的文件，用英文半角逗号分隔，只能是ascii字符，\
                        //每一项的长度不超过5，如：jpg,bmp,png,jpeg 会返回包含上述扩展的文档（如1.jpg）。文件夹不会被过滤。请用小写。
    QString sort_by;    //enum(date, name, size)	默认空，不排序。 在分页（page!=0）的时候才有效。
                        //排序依据，time - 修改日期 name - 文件名 size - 文件大小
                        //在前面加上字母"r"的时候代表倒序，如：sort_by = rsize 表示从大到小排序。
   // QString list;       //string/JSON bool	默认true。当路径指向是文件夹时，会返回其包含的子文件

}struMetaDataPara;

typedef enum EnumTabWidget
{
    TAB_GET_META=0,
    TAB_SHARE,
    TAB_CREATE_NEW,
    TAB_DEL,
    TAB_MOVE_CPOY,
    TAB_UPLOAD,
    TAB_DWNLOAD
}ETabWidget;

class kpDemo : public QMainWindow
{
    Q_OBJECT

public:
    explicit kpDemo(QWidget *parent = 0);    
    ~kpDemo();
    QString buildReqTknUrl(QString &consKeyStr,QString &consKeySecretStr);
    QString buildAcesTknUrl(QString &consKeyStr,QString &consKeySecretStr,\
                              QString &tmpTknStr,QString &tmpTknSercetStr);
    QString buildUsrInfoUrl(QString &consKeyStr,QString &consKeySecretStr,\
                            QString &oauTknStr,QString &oauTknSercetStr);
    QString buildMetaDataUrl(QString &consKeyStr,QString &consKeySecretStr,\
                             QString &oauTknStr,QString &oauTknSercetStr,\
                             struMetaDataPara &dataPara,bool &isAppPath,QString &dataPath);
    QString buildCreateFolderUrl(QString &consKeyStr,QString &consKeySecretStr,\
                                 QString &oauTknStr,QString &oauTknSercetStr,\
                                 bool &isAppPath,QString &folderName);
    QString buildDelFileUrl(QString &consKeyStr,QString &consKeySecretStr,\
                            QString &oauTknStr,QString &oauTknSercetStr,\
                            bool &isAppPath,QString &fileName,bool &toRecyle);
    QString buildMoveFileUrl(QString &consKeyStr,QString &consKeySecretStr,\
                             QString &oauTknStr,QString &oauTknSercetStr,\
                             bool &isAppPath,QString &fromPath,QString &toPath);
    QString buildCopyFileUrl(QString &consKeyStr,QString &consKeySecretStr,\
                             QString &oauTknStr,QString &oauTknSercetStr,\
                             bool &isAppPath,QString &fromPath,QString &toPath);
    QString buildUploadLocateUrl(QString &consKeyStr,QString &consKeySecretStr,\
                                 QString &oauTknStr,QString &oauTknSercetStr);
    QString buildUploadFileUrl(QString &consKeyStr,QString &consKeySecretStr,\
                               QString &oauTknStr,QString &oauTknSercetStr,\
                               const bool &isOverWrite, const bool &isAppPath,\
                               const QString &toPath,QString &preStr);
    QString buildDownFileUrl(QString &consKeyStr,QString &consKeySecretStr,\
                             QString &oauTknStr,QString &oauTknSercetStr,\
                             bool &isAppPath,QString &fromPath);
    QString buildShareFileUrl(QString &consKeyStr,QString &consKeySecretStr,\
                              QString &oauTknStr,QString &oauTknSercetStr,\
                              bool &isAppPath,QString &dataPath);
private:
    Ui::kpDemo *ui;
    void    init();
    void    judgeCfgFile();
    QString getReqTknSignature(QString &paraStr,QString &consKeyStr,QString &consKeySecretStr);
    QString getAcesTknSignature(QString &paraStr,QString &consKeyStr,QString &consKeySecretStr,\
                                QString &tmpTknStr,QString &tmpTknSercetStr);
    QString getUsrInfoSignature(QString &paraStr,QString &consKeyStr,QString &consKeySecretStr,\
                                QString &oauTknStr,QString &oauTknSercetStr);
    QString getMetaDataSignature(QString &paraStr,QString &consKeyStr,QString &consKeySecretStr,\
                                 QString &oauTknStr,QString &oauTknSercetStr,\
                                 struMetaDataPara &dataPara,QString &newGetMetaUrl);
    QString getCreateFolderSignature(QString &paraStr,QString &consKeyStr,QString &consKeySecretStr,\
                                     QString &oauTknStr,QString &oauTknSercetStr,\
                                     bool &isAppPath,QString &folderName);
    QString getDelFileSignature(QString &paraStr,QString &consKeyStr,QString &consKeySecretStr,\
                                QString &oauTknStr,QString &oauTknSercetStr,\
                                bool &isAppPath,QString &fileName,bool &toRecyle);
    QString getMoveFileSignature(QString &paraStr,QString &consKeyStr,QString &consKeySecretStr,\
                                 QString &oauTknStr,QString &oauTknSercetStr,\
                                 bool &isAppPath,QString &fromPath,QString &toPath);
    QString getCopyFileSignature(QString &paraStr,QString &consKeyStr,QString &consKeySecretStr,\
                                 QString &oauTknStr,QString &oauTknSercetStr,\
                                 bool &isAppPath,QString &fromPath,QString &toPath);
    QString getUploadLocateSignature(QString &paraStr,QString &consKeyStr,QString &consKeySecretStr,\
                                     QString &oauTknStr,QString &oauTknSercetStr);
    QString getUploadFileSignature(QString &paraStr,QString &consKeyStr,QString &consKeySecretStr,\
                                   QString &oauTknStr,QString &oauTknSercetStr,\
                                   const bool &isOverWrite,const bool &isAppPath,\
                                   const QString &toPath,QString &fullUrl);
    QString getDownFileSignature(QString &paraStr,QString &consKeyStr,QString &consKeySecretStr,\
                                 QString &oauTknStr,QString &oauTknSercetStr,\
                                 bool &isAppPath,QString &fromPath);
    QString getShareFileSignature(QString &paraStr,QString &consKeyStr,QString &consKeySecretStr,\
                                  QString &oauTknStr,QString &oauTknSercetStr,\
                                  QString &newShareFileUrl);

    void    procTmpToken(QString &tmpToken,QString &tmpTokenSecret,const QByteArray &buf);
    void    setAuthorise(const QString &tmpToken);
    void    showErrInfo(int ret,QByteArray &retErrInfo);
    void    addTknNonceTimes(QString &paraStr,QString &consKeyStr);
    void    procAuToken(struAuToken &struTkn,const QByteArray &buf);
    void    procUsrInfo(struUsrInfo &usrInfo,const QByteArray &buf);
    void    procMetaData(const QByteArray &buf);
    void    procCreateFolder(const QByteArray &buf);
    void    procDelFile(const QByteArray &buf);
    void    procMvFile(const QByteArray &buf);
    void    procShareFile(const QByteArray &buf);
    void    writeCfg();
    int     openFile(QByteArray &buf,const QString &filePath);
    void    buildMultiPart(QByteArray &data);
    void    setQuotaBar();
    QString mTmpTokenSecret;
    QString mTmpToken;
    QString mReqTokenSecret;
    QString mUserDirStr;
    QString mUploadNode;
    bool    mIsAppPath;
    QUrl    inputUrl;
    QLabel  *lMsgLabel;
    QFile   cfgFile;
    QNetworkAccessManager *manager;
    QNetworkRequest       request;
    QNetworkRequest       uploadRequest;
    QNetworkReply         *mReqTknReply;
    QNetworkReply         *mAcesTknReply;
    QNetworkReply         *mUsrInfoReply;
    QNetworkReply         *mMetaDataReply;
    QNetworkReply         *mCreateFolderReply;
    QNetworkReply         *mDelFileReply;
    QNetworkReply         *mMvFileReply;
    QNetworkReply         *mCpFileReply;
    QNetworkReply         *mUploadLocateReply;
    QNetworkReply         *mUploadFileReply;
    QNetworkReply         *mDownloadFileReply;
    QNetworkReply         *mDwnFileRealReply;
    QNetworkReply         *mShareFileReply;
    int     mAuUrlChangeNum;
    bool    mAuthoriseOk;
    struAuToken mAuTokenInfo;
    struUsrInfo mUsrInfoStru;
    QByteArray mDwnFileBuf;
    QFile *dwnFile;

signals:
    void logInResult(bool ret);
public slots:
    void sendRequestToken();
    void reqTknReplyFinished();

    void getAuthoriseResult(bool isFinished);

    void acesTknReplyFinished();
    void sendAccessToken(bool logInRet);

    void reqUsrInfo();
    void usrInfoReplyFinished();

    void reqMetaData();
    void metaDataReplyFinished();

    void reqCreateFloder();
    void createFolderReplyFinished();

    void reqDelFile();
    void delFileReplyFinished();

    void reqMoveFile();
    void moveFileReplyFinished();

    void reqCopyFile();
    void copyFileReplyFinished();

    void reqUploadFile();
    void getUploadLocateFinished();
    void upLoadFileReplyFinished();
    void showUploadProgress(qint64 bytesWrite, qint64 totalBytes);

    void reqDownLoadFile();
    void downLoadFileReplyFinished();
    void dwnFileRealReadReady();
    void dwnFileRealReplyFinished();
    void showDownProgress(qint64 bytesRead, qint64 totalBytes);

    void reqShareFile();
    void shareFileReadReady();
    void shareFileReplyFinished();

    void openUploadFile();
    void saveDwnloadFile();
};

#endif // KPDEMO_H
